#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time   :2021/12/8
# @Author :CJX
# @File   :DHT11.py
import RPi.GPIO as GPIO
import time
import pymysql
from DFRobot_DHT20 import *

IIC_MODE = 0x01  # default use IIC1
IIC_ADDRESS = 0x38  # default i2c device address
dht20 = DFRobot_DHT20(IIC_MODE, IIC_ADDRESS)
dht20.begin()


def DHT20():
    con = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    address = "DHT11"
    temperature = int(float('%-3.1f' % dht20.get_temperature()))
    humidity = int(float('%-3.1f' % dht20.get_humidity()))
    # print("温度是： ", temperature, "湿度是： ", humidity)
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany(
        "INSERT DHT20(address,time,temperature,humidity) VALUE(%s,%s,%s,%s)",
        [(address, time1, temperature, humidity)])
    con.commit()
    cur.close()
    return temperature, humidity


print(DHT20()[0])
